package com.vichat.api.service.impl;

import com.vichat.api.entity.*;
import com.vichat.api.service.IApiService;
import com.vichat.common.util.NumberUtils;
import com.vichat.core.dao.BaseDaoI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by wangluyin on 2017/12/18.
 */

@Service("apiService")
public class ApiServiceImpl implements IApiService {

    @Autowired
    private BaseDaoI<RequestBusiRecord> requestBusiRecordDao;
    @Autowired
    private BaseDaoI<ResponseBusiRecord> responseBusiRecordDao;

    @Autowired
    private BaseDaoI<HRequestBusiRecord> hRequestBusiRecordDao;
    @Autowired
    private BaseDaoI<ApiBusiRecord> apiBusiRecordDao;
    @Autowired
    private BaseDaoI<HResponseBusiRecord> hResponseBusiRecordDao;
    @Autowired
    private BaseDaoI<VichatJson> vichatJsonDao;

    @Override
    public void saveRequestBusiRecord(RequestBusiRecord requestBusiRecord) throws Exception {
        VichatJson json = new VichatJson();
        json.setJsonStr(requestBusiRecord.getRequestContent());
        vichatJsonDao.save(json);
        requestBusiRecord.setJsonId(json.getJsonId());
        requestBusiRecordDao.save(requestBusiRecord);
    }

    @Override
    public void saveResponseBusiRecord(ResponseBusiRecord responseBusiRecord) throws Exception {
        VichatJson json = new VichatJson();
        json.setJsonStr(responseBusiRecord.getResponseContent());
        vichatJsonDao.save(json);
        responseBusiRecord.setJsonId(json.getJsonId());
        responseBusiRecordDao.save(responseBusiRecord);
    }

    @Override
    public void updateResponseBusiRecord(ResponseBusiRecord responseBusiRecord) throws Exception {
        responseBusiRecordDao.update(responseBusiRecord);
    }
    public void backRequestBusiRecord(RequestBusiRecord source) throws Exception {
        HRequestBusiRecord history = new HRequestBusiRecord();
        BeanUtils.copyProperties(source,history);
        hRequestBusiRecordDao.save(history);
        if(NumberUtils.isNotNullOrZero(source.getRequestId())) {
            requestBusiRecordDao.deleted(source);
        }
    }
    public void backResponseBusiRecord(ResponseBusiRecord source) throws Exception {
        HResponseBusiRecord history = new HResponseBusiRecord();
        BeanUtils.copyProperties(source,history);
        hResponseBusiRecordDao.save(history);
        if(NumberUtils.isNotNullOrZero(source.getResponseId())) {
            responseBusiRecordDao.deleted(source);
        }
    }

    @Override
    public List<RequestBusiRecord> fetchTodoRequest(int rows) throws Exception {
        String hql = "from RequestBusiRecord where state = 1 order by requestId";
        List<RequestBusiRecord> list = requestBusiRecordDao.find(hql,1,rows);
        if(list!=null && list.size()>0){
            for (RequestBusiRecord record:list  ) {
                record.setState(2L);
                requestBusiRecordDao.update(record);
            }
        }

        return list;
    }

    @Override
    public void saveApiBusiRecord(ApiBusiRecord apiBusiRecord) throws Exception {
        apiBusiRecordDao.save(apiBusiRecord);
    }

    @Override
    public List<ResponseBusiRecord> fetchTodoResponse(int rows) throws Exception {
        String hql = "from ResponseBusiRecord where state = 1 order by responseId";
        List<ResponseBusiRecord> list = responseBusiRecordDao.find(hql,1,rows);
        if(list!=null && list.size()>0){
            for (ResponseBusiRecord record:list  ) {
                record.setState(2L);
                responseBusiRecordDao.update(record);
            }
        }
        return list;
    }


    public VichatJson getVichatJson(long jsonId)throws Exception {
        return vichatJsonDao.get(VichatJson.class,jsonId);
    }

    @Override
    public ResponseBusiRecord queryResponse(String transactionId) throws Exception {
        String hql = "from ResponseBusiRecord where transactionId =:transactionId order by responseId desc";
        HashMap params = new HashMap();
        params.put("transactionId",transactionId);
        List<ResponseBusiRecord> records = responseBusiRecordDao.find(hql,params,1,1);
        if(records!=null && records.size()>0){
            return records.get(0);
        }
        return null;
    }

    public HResponseBusiRecord queryHResponse(String transactionId) throws Exception {
        String hql = "from HResponseBusiRecord where transactionId =:transactionId order by responseId desc";
        HashMap params = new HashMap();
        params.put("transactionId",transactionId);
        List<HResponseBusiRecord> records = hResponseBusiRecordDao.find(hql,params,1,1);
        if(records!=null && records.size()>0){
            return records.get(0);
        }
        return null;
    }
}
